package com.czh.springboot04.MyController;

import com.czh.springboot04.entity.CourseBuy;
import com.czh.springboot04.service.CourseBuyService;
import com.czh.springboot04.service.serviceimpl.CourseBuyServiceImpl;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/CourseBuyController")
public class CourseBuyController {
    @Autowired
    DataSource dataSource;
    @Autowired
    CourseBuyService courseBuyService = new CourseBuyServiceImpl();

    @PostMapping("/insertCourseBuy")
    public R insertCourseBuy(@RequestBody CourseBuy courseBuy){
        System.out.println(courseBuy);
        courseBuy.setBuyTime(new Date());
        int i = courseBuyService.insertCourseBuy(courseBuy);
        if (i!=0){
            return R.ok("购买成功！");
        }else {
            return R.fail("购买失败！");
        }
    }
    @PutMapping("/updateCourseBuyById")
    public R updateCourseBuyById(@RequestBody CourseBuy courseBuy){
        int i = courseBuyService.updateCourseBuyById(courseBuy);
        if (i!=0){
            return R.ok("修改成功！");
        }else {
            return R.fail("修改失败！");
        }
    }
    @GetMapping("/selectCourseBuyById")
    public R selectCourseBuyById(Integer buyId){
        CourseBuy courseBuy = courseBuyService.selectCourseBuyById(buyId);
        if (courseBuy!=null){
            return R.ok(courseBuy);
        }else {
            return R.fail("查询失败！");
        }
    }

    @GetMapping("/selectCourseBuyAll")
    public R selectCourseBuyAll(Integer courseId,
                                Integer memberId,
                                double minbuyPrice,
                                double maxbuyPrice,
                                Integer buyState,
                                @RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                @RequestParam(required = false,defaultValue = "10") Integer pageSize){
        PageInfo<CourseBuy> courseBuyPageInfo = courseBuyService.selectCourseBuyAll(courseId, memberId, minbuyPrice,maxbuyPrice,pageNum, pageSize, buyState);
        if (courseBuyPageInfo!=null){
            return R.ok(courseBuyPageInfo);
        }else {
            return R.fail("查询失败！");
        }
    }

    @DeleteMapping("/deleteCourseBuyById/{buyId}")
    public R deleteCourseBuyById(@PathVariable("buyId") Integer buyId){
        int i = courseBuyService.deleteCourseBuyById(buyId);
        if (i!=0){
            return R.ok("删除成功！");
        }else {
            return R.fail("删除失败！");
        }
    }

    @DeleteMapping("/deleteCourseBuyByList")
    public R deleteCourseBuyByList(@RequestBody List<Integer> buyIdList){
        int i = courseBuyService.deleteCourseBuyByList(buyIdList);
        if (i!=0){
            return R.ok("批量删除成功！");
        } else {
            return R.fail("批量删除失败！");
        }
    }
    //查看所有购买记录
    @GetMapping("selectCourseBuyList")
    public R selectCourseBuyList(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                 @RequestParam(required = false,defaultValue = "10") Integer pageSize){
        PageInfo<CourseBuy> courseBuyPageInfo = courseBuyService.selectCourseBuyList(pageNum, pageSize);
        if (courseBuyPageInfo!=null){
            return R.ok(courseBuyPageInfo);
        }else {
            return R.fail("查询失败！");
        }
    }
    //根据课程id和用户id查询购买记录
    @GetMapping("selectCourseBuyByCourseId")
    public R selectCourseBuyByCourseId(int courseId,int memberId){
        System.out.println("courseId="+ courseId+ '=' + memberId);
        CourseBuy courseBuy = courseBuyService.selectCourseBuyByCourseId(courseId, memberId);
        if (courseBuy!=null){
            return R.ok(courseBuy);
        }
        return R.fail("查询失败！");
    }
    @GetMapping("/selectByMid")
    public R selectByMid(
                                @RequestParam(required = false,defaultValue = "1") int pageNum,
                                @RequestParam(required = false,defaultValue = "10") int pageSize,
                                 int memberId){
        PageInfo<CourseBuy> pageInfo = courseBuyService.selectByMId(pageNum, pageSize,memberId);
        if (pageInfo!=null){
            return R.ok(pageInfo);
        }else {
            return R.fail("查询失败！");
        }
    }

}
